cmake_minimum_required(VERSION 3.16)
project(gtk3lyrics)

# 设置C和C++标准
set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 14)

# 设置编译标志，与Makefile保持一致
# 在MinGW-w64环境中移除LTO选项以避免对象文件格式兼容性问题
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall -O2 -DDDB_LYRICS_EXPORT_LIB -D_GNU_SOURCE -fPIC -fvisibility=hidden")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -O2 -fPIC -fvisibility=hidden -DDDB_LYRICS_EXPORT_LIB")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")

# 查找必要的包
find_package(PkgConfig REQUIRED)

# 查找GTK3和taglib
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
pkg_check_modules(TAGLIB REQUIRED taglib)

# 查找curl
find_package(CURL REQUIRED)

# 包含目录
include_directories(${CMAKE_SOURCE_DIR}/include)
include_directories(${GTK3_INCLUDE_DIRS} ${TAGLIB_INCLUDE_DIRS})
link_directories(${GTK3_LIBRARY_DIRS} ${TAGLIB_LIBRARY_DIRS})

# 添加定义
add_definitions(${GTK3_CFLAGS_OTHER} ${TAGLIB_CFLAGS_OTHER})

# 生成资源文件
set(RESOURCES_XML ${CMAKE_SOURCE_DIR}/src/resources.xml)
set(RESOURCES_C ${CMAKE_CURRENT_BINARY_DIR}/resources.c)
set(RESOURCES_H ${CMAKE_CURRENT_BINARY_DIR}/resources.h)

# 使用glib-compile-resources生成资源文件
add_custom_command(
    OUTPUT ${RESOURCES_C} ${RESOURCES_H}
    COMMAND glib-compile-resources --generate-source --target=${RESOURCES_C} ${RESOURCES_XML}
    COMMAND glib-compile-resources --generate-header --target=${RESOURCES_H} ${RESOURCES_XML}
    DEPENDS ${RESOURCES_XML}
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    COMMENT "Generating resource files"
)

# 创建自定义目标来确保资源文件被生成
add_custom_target(generate_resources DEPENDS ${RESOURCES_C} ${RESOURCES_H})

# 公共源文件列表
set(COMMON_SOURCES
    src/kugou.cpp
    src/base64.cpp
    src/LyricUi.cpp
    src/config_dialog.cpp
    src/LyricTool.cpp
    src/utils.cpp
    src/lyric_log.cpp
    ${RESOURCES_C}
)

# 公共头文件列表
set(COMMON_HEADERS
    src/json.hpp
    src/gettext.h
    src/kugou.h
    src/base64.h
    src/LyricUi.h
    src/config_dialog.h
    src/LyricTool.h
    src/utils.h
    src/main.h
    src/lyric_log.h
    ${RESOURCES_H}
)

# Demo程序的源文件
set(DEMO_SOURCES
    src/main.cpp
    ${COMMON_SOURCES}
)

# Plugin的源文件
set(PLUGIN_SOURCES
    src/plugin.cpp
    ${COMMON_SOURCES}
)

# 创建可执行文件 (lyric_demo)
add_executable(lyric_demo ${DEMO_SOURCES} ${COMMON_HEADERS})
target_link_libraries(lyric_demo 
    ${GTK3_LIBRARIES} 
    ${TAGLIB_LIBRARIES} 
    CURL::libcurl
)
add_dependencies(lyric_demo generate_resources)

# 创建共享库 (ddb_lyricbar_gtk3.so)
add_library(ddb_lyricbar_gtk3 SHARED ${PLUGIN_SOURCES} ${COMMON_HEADERS})
target_link_libraries(ddb_lyricbar_gtk3 
    ${GTK3_LIBRARIES} 
    ${TAGLIB_LIBRARIES} 
    CURL::libcurl
)
add_dependencies(ddb_lyricbar_gtk3 generate_resources)

# 设置共享库的属性
if(WIN32)
    set_target_properties(ddb_lyricbar_gtk3 PROPERTIES
        OUTPUT_NAME "ddb_lyricbar_gtk3"
        PREFIX ""
        SUFFIX ".dll"
    )
    set_target_properties(lyric_demo PROPERTIES
        OUTPUT_NAME "lyric_demo"
        SUFFIX ".exe"
    )
else()
    set_target_properties(ddb_lyricbar_gtk3 PROPERTIES
        OUTPUT_NAME "ddb_lyricbar_gtk3"
        PREFIX ""
        SUFFIX ".so"
    )
endif()

# 安装规则
install(TARGETS lyric_demo ddb_lyricbar_gtk3
    RUNTIME DESTINATION bin
    LIBRARY DESTINATION lib/deadbeef
)

# 安装国际化文件
if(EXISTS ${CMAKE_SOURCE_DIR}/gettext/ru/deadbeef-lyricbar.po)
    install(FILES gettext/ru/deadbeef-lyricbar.po 
            DESTINATION share/locale/ru/LC_MESSAGES
            RENAME deadbeef-lyricbar.mo)
endif()

# 添加清理目标
add_custom_target(clean-all
    COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_BINARY_DIR}/CMakeFiles
    COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_BINARY_DIR}/CMakeCache.txt
    COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_BINARY_DIR}/cmake_install.cmake
    COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_BINARY_DIR}/Makefile
    COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_BINARY_DIR}/*.so
    COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_BINARY_DIR}/*.dll
    COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_BINARY_DIR}/*.exe
    COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_BINARY_DIR}/*.o
    COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_BINARY_DIR}/resources.c
    COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_BINARY_DIR}/resources.h
    COMMENT "Cleaning all build files"
)
